package com.qst.controller;


import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.RequestUtil;
import com.qst.entity.AssessmentType;
import com.qst.entity.PersonalityDimension;
import com.qst.service.AssessmentService;
import com.qst.service.DimensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/dimension")
@SuppressWarnings("all")
public class DimensionController extends BaseServlet{
    /**
     * @Qualifier注解 用于指定注入的bean
     * assessmentServiceImpl 和 assessmentServiceSsmImpl 有共同的接口 AssessmentService
     * 但是他们的实现类不同，所以需要指定注入哪一个
     */
    @Autowired
    @Qualifier("dimensionServiceSsmImpl")
    DimensionService dimensionService;

    @Autowired
    @Qualifier("assessmentServiceSsmImpl")
    AssessmentService assessmentService;


    @RequestMapping("/view.action")
    public String view(HttpServletRequest req, HttpServletResponse resp){
        PersonalityDimension kp = dimensionService.findDimensionById(RequestUtil.getInt(req, "id"));
        req.setAttribute("dimension", kp);
//        WebUtil.forward(req, resp, "/dimension/view.jsp");
        return "/dimension/view";
    }

    @RequestMapping("/list.action")
    public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String submitButton = RequestUtil.getString(req, "create");
        if (submitButton.length() > 0)// 添加操作
        {
            return  doCreate(req, resp);
        } else {
            return  doList(req, resp);
        }
    }

    private String doCreate(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int assessmentId = RequestUtil.getInt(req, "id");
        AssessmentType assessment = assessmentService.findAssessmentById(assessmentId);
        req.setAttribute("assessment", assessment);
        //WebUtil.forward(req, resp, "/dimension/create.jsp");
        return "/dimension/create";
    }

    private String doList(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setAttribute("assessmentList", assessmentService.findAllAssessment());
        int id = RequestUtil.getInt(req, "id");
        List<PersonalityDimension> dimensionList = dimensionService
                .findDimensionByAssessment(id);
        req.setAttribute("dimensionList", dimensionList);
//       WebUtil.forward(req, resp, "/dimension/list.jsp");
        return "/dimension/list";
    }


   @RequestMapping("/delete.action")
   public String delete(HttpServletRequest req, HttpServletResponse resp){
       int id = RequestUtil.getInt(req, "id");
       try {
           dimensionService.deleteDimension(id);
           addMessage(req, "性格维度已删除.");
       } catch (ExamException ex) {
           ex.printStackTrace();
           addError(req, ex.getMessage());
       }
//       WebUtil.redirect(req, resp, "/dimension/list.action?id=" + RequestUtil.getInt(req, "assessmentId"));
       return "redirect:/dimension/list.action?id=" + RequestUtil.getInt(req, "assessmentId");
   }


    @RequestMapping("/edit.action")
    public String edit(HttpServletRequest req, HttpServletResponse resp)  {
        PersonalityDimension kp = dimensionService.findDimensionById(RequestUtil.getInt(req, "id"));
        req.setAttribute("dimension", kp);
//        WebUtil.forward(req, resp, "/dimension/edit.jsp");
        return "/dimension/edit";

    }

    @RequestMapping("/save.action")
    public String save(HttpServletRequest req, HttpServletResponse resp) {
        PersonalityDimension kp = new PersonalityDimension();
        kp.setTitle(RequestUtil.getString(req, "title"));
        kp.setDepict(RequestUtil.getString(req, "depict"));
        kp.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
        try {
            dimensionService.saveDimension(kp);
            addMessage(req, "性格维度信息已保存到数据库");
            return "redirect:/dimension/view.action?id=" + kp.getId();
        } catch (ExamException ex) {
            addError(req, ex.getMessage());
            System.out.println(ex.getMessage());
            req.setAttribute("dimension", kp);
//            WebUtil.forward(req, resp, "/dimension/create.jsp");
         return "/dimension/create";
        }
    }

    @RequestMapping("/update.action")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
         PersonalityDimension kp = new PersonalityDimension();
         kp.setId(RequestUtil.getInt(req, "id"));
         kp.setTitle(RequestUtil.getString(req, "title"));
         kp.setDepict(RequestUtil.getString(req, "depict"));
         kp.setAssessmentId(RequestUtil.getInt(req, "assessmentId"));
         try {
             dimensionService.updateDimension(kp);
             addMessage(req, "性格维度信息已保存到数据库");
//             WebUtil.redirect(req, resp, "/dimension/view.action?id=" + kp.getId());
             return "redirect:/dimension/view.action?id="+ kp.getId();
         } catch (ExamException ex) {
             addError(req, ex.getMessage());
             req.setAttribute("dimension", kp);
             req.setAttribute("assessment", assessmentService.findAssessmentById(kp.getAssessmentId()));
//             WebUtil.forward(req, resp, "/dimension/edit.jsp");
             return "/dimension/edit";
         }
     }
}
